package cn.edu.sasu.service.Impl;

import cn.edu.sasu.common.base.LayuiRespResult;
import cn.edu.sasu.common.base.RespResult;
import cn.edu.sasu.common.constants.Constant;
import cn.edu.sasu.domain.entity.CaseManager;
import cn.edu.sasu.domain.vo.CaseManagervo;
import cn.edu.sasu.mapper.CaseManagerDao;
import cn.edu.sasu.service.CaseManagerService;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * (CaseManager)表服务实现类
 *
 * @author makejava
 * @since 2024-04-28 09:42:50
 */
@Service("caseManagerService")
public class CaseManagerServiceImpl implements CaseManagerService {
    @Resource
    private CaseManagerDao caseManagerDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public CaseManager queryById(Integer id) {
        return this.caseManagerDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param caseManager 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */


    /**
     * 新增数据
     *
     * @param caseManager 实例对象
     * @return 实例对象
     */
    @Override
    public CaseManager insert(CaseManager caseManager) {
        this.caseManagerDao.insert(caseManager);
        return caseManager;
    }

    /**
     * 修改数据
     *
     * @param caseManager 实例对象
     * @return 实例对象
     */
    @Override
    public CaseManager update(CaseManager caseManager) {
        this.caseManagerDao.update(caseManager);
        return this.queryById(caseManager.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.caseManagerDao.deleteById(id) > 0;
    }

    @Override
    public LayuiRespResult caselist(Integer page, Integer limit) {
        Integer offset = (page - 1) * limit;
        Integer rows = limit;
        List<CaseManager> sysDepts = caseManagerDao.hospitalList(Constant.DELETE_FLAG_YES,offset,rows);
        Integer totalSize = caseManagerDao.countHospitals(Constant.DELETE_FLAG_YES);
        for (CaseManager sysDept : sysDepts) {
           if (sysDept.getCancelstatus().equals("2")){
               sysDept.setCancelstatus("已取消");
           }else {
               sysDept.setCancelstatus("未取消");
           }
           if (sysDept.getStatus().equals("0")){
               sysDept.setStatus("探讨中");
           }else {
               sysDept.setStatus("探讨结束");
           }
        }
        return LayuiRespResult.success(totalSize,sysDepts);
    }

    @Override
    public RespResult batchdel(Integer[] ids) {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedDateTime = now.format(formatter);
        int count=  0;
        for (Integer id : ids) {
                int cancel = caseManagerDao.findCancel(id, Constant.DELETE_FLAG_YES);
                if (cancel > 0) {
                    continue;
                } else {
                    caseManagerDao.updateCancel(id, Constant.DELETE_FLAG_YES, formattedDateTime);
                    count++;
                }
            }
           if (count>0){
               return RespResult.success("取消成功",200);
           }else {
               return RespResult.error("已被取消");
           }
        }

    @Override
    public List<CaseManager> search(CaseManagervo caseManager) {
        return caseManagerDao.search1(caseManager);
    }

    @Override
    public LayuiRespResult findAllCase(Integer page, Integer limit, Integer doctorId) {
        Integer offset = (page - 1) * limit;
        Integer rows = limit;
        List<CaseManager> sysDepts = caseManagerDao.findAllCase(offset, rows,doctorId);
        Integer totalSize = caseManagerDao.countAllCase(doctorId);
        for (CaseManager sysDept : sysDepts) {
            if (sysDept.getCancelstatus().equals("2")){
                sysDept.setCancelstatus("已取消");
            }else {
                sysDept.setCancelstatus("未取消");
            }
            if (sysDept.getStatus().equals("0")){
                sysDept.setStatus("探讨中");
            }else {
                sysDept.setStatus("探讨结束");
            }
        }
        return LayuiRespResult.success(totalSize,sysDepts);
    }


}

